
package lichen.pages.control;

import lichen.entities.user.User;
import lichen.pages.base.AbstractControlPage;
import lichen.services.UserService;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.PasswordField;
import org.apache.tapestry5.corelib.components.Select;
import org.apache.tapestry5.corelib.components.TextField;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.springframework.transaction.annotation.Transactional;


/**
 * 修改密码.
 * @author <a href=mailto:xiafei114@gmail.com>xiafei114</a>
 * @version $Revision: 225 $
 * @since 0.0.3
 */
public class FormChangePersonInfo extends AbstractControlPage {
	
	
//	 @Inject  
//     private Block _fred;  
//   
//     @Inject  
//     private Block _barney;  
//   
//     // Blocks not injected until page load, so must lazily initialize the map.  
//     @Retain  
//     private Map _blocks = null;  
//	
//	 @Persist  
//     private String _blockName;  
//   
//     public Block getBlockToRender()  
//     {  
//         if (_blocks == null)  
//         {  
//             _blocks = CollectionFactory.newMap();  
//             _blocks.put("fred", _fred);  
//             _blocks.put("barney", _barney);  
//         }  
//   
//         return (Block) _blocks.get(_blockName);  
//     }  
//   
//     public String getBlockName()  
//     {  
//         return _blockName;  
//     }  
//   
//     public void setBlockName(String blockName)  
//     {  
//         _blockName = blockName;  
//     }  
	
	
	
	
	
	
	
	
	
	@Transactional(readOnly=false)
	Object onActionFromUserForm(){
		User user = getUser();
		
		if(this.getOldPassword() == null){
			this.setOldPassword(new String());
		}
		
		if(this.getNewPassword() == null){
			this.setNewPassword(new String());
		}
		
		if(this.getRefrainPassword() == null){
			this.setRefrainPassword(new String());
		}
		
		if((this.getNewPassword().length() != 0 || this.getRefrainPassword().length() != 0) && this.getOldPassword().length() == 0){
			_userForm.recordError(oldPasswordField, "请输入老密码!");
		}
		
		if(getOldPassword().length() != 0){
			if( ! this.getNewPassword().equals(this.getRefrainPassword())){
				_userForm.recordError(newPasswordField, "新密码两次输入不一致!");
			}
			
			if( this.getNewPassword().length() != 0){
				_userForm.recordError(newPasswordField, "密码不能是空!");
			}
			
			if(!user.getPassword().equals(DigestUtils.md5Hex(getOldPassword()))){
				_userForm.recordError(oldPasswordField, "输入的老密码错误!");
			}
		}

		if(!_userForm.isValid()){
			return this;
		}
		
		if(getOldPassword().length() != 0){
			user.setPassword(DigestUtils.md5Hex(this.getNewPassword()));
		}
		
		this.getEntityService().saveOrUpdate(user);
		
		return viewControl;
	}
	
	@InjectPage
	private ViewControl viewControl;
	
	@Inject
	private UserService _userService;
	
	@Persist
	private User _user;
	
	void onActivate(){
		if(_user == null)
			_user = this.getUser();
	}
	
	protected UserService getUserService(){
		return _userService;
	}
	
	@Component
    private Form _userForm;
	
	private String oldPassword;
	
	private String newPassword;
	
	private String refrainPassword;
	
	@Component(parameters={"value=user.email","validate=email"})
	private TextField emailField;

	@SuppressWarnings("unused")
	@Component(parameters={"value=oldPassword"})
	private PasswordField oldPasswordField;
	@SuppressWarnings("unused")
	@Component(parameters={"value=newPassword"})
	private PasswordField newPasswordField;
	@SuppressWarnings("unused")
	@Component(parameters={"value=refrainPassword"})
	private PasswordField refrainPasswordField;
	
	@Component(parameters={"value=user.qq"})
	private TextField qqField;
	
	@Component(parameters={"value=user.icq"})
	private TextField icqField;
	
	@Component(parameters={"value=user.yahoo"})
	private TextField yahooField;
	
	@Component(parameters={"value=user.msn"})
	private TextField msnField;
	
	@Component(parameters={"value=user.comefrom"})
	private TextField comefromField;
	
	@Component(parameters={"value=user.homepage"})
	private TextField homepageField;
	
	@Component(parameters={"value=user.sex","model='不详,男性,女性'"})
	private Select sexField;
	
	@Component(parameters={"value=user.birthday"})
	private TextField birthdayField;
	
	@Component(parameters={"value=user.idiograph"})
	private TextField idiographField;
	
	@Component(parameters={"value=user.headPortrait"})
	private TextField headPortraitField;
	
	@Component(parameters={"value=user.style"})
	private TextField styleField;

	@Component(parameters={"value=user.timeZone"})
	private TextField timeZoneField;

//	/**
//	 * @return
//	 * @since 0.0.3
//	 */
//	public SelectModel getPropertySelectModel(){
//		return PropertySelectModelUtil.getSelectModel(Arrays.asList(new String [] {"不详","男性","女性"}));
//	}
	
	/**
	 * @return Returns the emailField.
	 */
	public TextField getEmailField() {
		return emailField;
	}

	/**
	 * @param emailField The emailField to set.
	 */
	public void setEmailField(TextField emailField) {
		this.emailField = emailField;
	}

	/**
	 * @return Returns the newPassword.
	 */
	public String getNewPassword() {
		return newPassword;
	}

	/**
	 * @param newPassword The newPassword to set.
	 */
	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	/**
	 * @return Returns the newPasswordField.
	 */
	public PasswordField getNewPasswordField() {
		return newPasswordField;
	}

	/**
	 * @param newPasswordField The newPasswordField to set.
	 */
	public void setNewPasswordField(PasswordField newPasswordField) {
		this.newPasswordField = newPasswordField;
	}

	/**
	 * @return Returns the oldPassword.
	 */
	public String getOldPassword() {
		return oldPassword;
	}

	/**
	 * @param oldPassword The oldPassword to set.
	 */
	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	/**
	 * @return Returns the oldPasswordField.
	 */
	public PasswordField getOldPasswordField() {
		return oldPasswordField;
	}

	/**
	 * @param oldPasswordField The oldPasswordField to set.
	 */
	public void setOldPasswordField(PasswordField oldPasswordField) {
		this.oldPasswordField = oldPasswordField;
	}

	/**
	 * @return Returns the refrainPassword.
	 */
	public String getRefrainPassword() {
		return refrainPassword;
	}

	/**
	 * @param refrainPassword The refrainPassword to set.
	 */
	public void setRefrainPassword(String refrainPassword) {
		this.refrainPassword = refrainPassword;
	}

	/**
	 * @return Returns the refrainPasswordField.
	 */
	public PasswordField getRefrainPasswordField() {
		return refrainPasswordField;
	}

	/**
	 * @param refrainPasswordField The refrainPasswordField to set.
	 */
	public void setRefrainPasswordField(PasswordField refrainPasswordField) {
		this.refrainPasswordField = refrainPasswordField;
	}

	/**
	 * @return Returns the birthdayField.
	 */
	public TextField getBirthdayField() {
		return birthdayField;
	}

	/**
	 * @param birthdayField The birthdayField to set.
	 */
	public void setBirthdayField(TextField birthdayField) {
		this.birthdayField = birthdayField;
	}

	/**
	 * @return Returns the comefromField.
	 */
	public TextField getComefromField() {
		return comefromField;
	}

	/**
	 * @param comefromField The comefromField to set.
	 */
	public void setComefromField(TextField comefromField) {
		this.comefromField = comefromField;
	}

	/**
	 * @return Returns the headPortraitField.
	 */
	public TextField getHeadPortraitField() {
		return headPortraitField;
	}

	/**
	 * @param headPortraitField The headPortraitField to set.
	 */
	public void setHeadPortraitField(TextField headPortraitField) {
		this.headPortraitField = headPortraitField;
	}

	/**
	 * @return Returns the homepageField.
	 */
	public TextField getHomepageField() {
		return homepageField;
	}

	/**
	 * @param homepageField The homepageField to set.
	 */
	public void setHomepageField(TextField homepageField) {
		this.homepageField = homepageField;
	}

	/**
	 * @return Returns the icqField.
	 */
	public TextField getIcqField() {
		return icqField;
	}

	/**
	 * @param icqField The icqField to set.
	 */
	public void setIcqField(TextField icqField) {
		this.icqField = icqField;
	}

	/**
	 * @return Returns the idiographField.
	 */
	public TextField getIdiographField() {
		return idiographField;
	}

	/**
	 * @param idiographField The idiographField to set.
	 */
	public void setIdiographField(TextField idiographField) {
		this.idiographField = idiographField;
	}

	/**
	 * @return Returns the msnField.
	 */
	public TextField getMsnField() {
		return msnField;
	}

	/**
	 * @param msnField The msnField to set.
	 */
	public void setMsnField(TextField msnField) {
		this.msnField = msnField;
	}

	/**
	 * @return Returns the qqField.
	 */
	public TextField getQqField() {
		return qqField;
	}

	/**
	 * @param qqField The qqField to set.
	 */
	public void setQqField(TextField qqField) {
		this.qqField = qqField;
	}

	/**
	 * @return Returns the sexField.
	 */
	public Select getSexField() {
		return sexField;
	}

	/**
	 * @param sexField The sexField to set.
	 */
	public void setSexField(Select sexField) {
		this.sexField = sexField;
	}

	/**
	 * @return Returns the styleField.
	 */
	public TextField getStyleField() {
		return styleField;
	}

	/**
	 * @param styleField The styleField to set.
	 */
	public void setStyleField(TextField styleField) {
		this.styleField = styleField;
	}

	/**
	 * @return Returns the timeZoneField.
	 */
	public TextField getTimeZoneField() {
		return timeZoneField;
	}

	/**
	 * @param timeZoneField The timeZoneField to set.
	 */
	public void setTimeZoneField(TextField timeZoneField) {
		this.timeZoneField = timeZoneField;
	}

	/**
	 * @return Returns the viewControl.
	 */
	public ViewControl getViewControl() {
		return viewControl;
	}

	/**
	 * @param viewControl The viewControl to set.
	 */
	public void setViewControl(ViewControl viewControl) {
		this.viewControl = viewControl;
	}

	/**
	 * @return Returns the yahooField.
	 */
	public TextField getYahooField() {
		return yahooField;
	}

	/**
	 * @param yahooField The yahooField to set.
	 */
	public void setYahooField(TextField yahooField) {
		this.yahooField = yahooField;
	}
	
	
}
